function [WaterChange,Status,SubStatus] = WaterController(OnlyReturnStatus,...
    SchemeStatus,Totalstatus,BallRatio,ZZ208,ZZ203A,ZZ203B,ReturnBin,ReturnBin_10min,...
    CurrentA,CurrentA_1min,CurrentA_2min,CurrentA_3min,...
    CurrentB,CurrentB_1min,CurrentB_2min,CurrentB_3min,...
    SpeedSetA,SpeedSetB,TotalWeight,...
    CurrentA_Average,CurrentB_Average,...
    DecisionMode,CurrentA_low,CurrentA_high,CurrentB_low,CurrentB_high)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% 1. 单压返料报警
% 2. 内配改变报警
% 3. 总料改变报警
% 4. 成球率，和粘结剂中用到的变量一样
% 5. ZZ208->DB305_322
% 6. ZZ203->DB305_218
% 7.  ZZ204->DB305_219
% 8.  返料仓->DB305_292，和粘结剂中用到的变量一样
% 9.  返料仓_10min，和粘结剂中用到的变量一样
% 10. 压球电流A->DB305_226
% 11. 压球电流A_1min
% 12. 压球电流A_2min（直接相减的差值，没有./2，以下相同）
% 13. 压球电流A_3min
% 14. 压球电流B->DB305_227
% 15. 压球电流B_1min
% 16. 压球电流B_2min
% 17. 压球电流B_3min
% 18. 压球机设定转速A -> DB305_228
% 19. 压球机设定转速B  -> DB305_229
% 20. 实际总料重
% 21. 压球电流A平均值：过去三小时平均值，去除空转停机
% 22. 压球电流B平均值：同A
% 23. 切换电流判断模式[1,2,3]：1是过去3小时的电流区间判断，2是机器学习，3是工人经验调整，还没写好3
% 24. 电流A稳定区间low 给工人填写准备的
% 25. 电流A稳定区间high 给工人填写准备的
% 26. 电流B稳定区间low 给工人填写准备的
% 27. 电流B稳定区间high 给工人填写准备的

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%出现异常值输出为NaN并报警
if(OnlyReturnStatus)
    WaterChange = NaN;
    Status = 6;
    SubStatus = 1;
    return;
end

if(SchemeStatus)
    WaterChange = NaN;
    Status = 6;
    SubStatus = 2;
    return;
end

if(Totalstatus)
    WaterChange = NaN;
    Status = 6;
    SubStatus = 3;
    return;
end
switch DecisionMode
    case 1  %过去三小时电流平均值判断
        CurrentShreholdA = [CurrentA_Average-10 CurrentA_Average+12];
        CurrentShreholdB = [CurrentB_Average-10 CurrentB_Average+12];
        %分别对A、B压球机进行判断，给出最终所在的区间

        %对A进行判断
        if (CurrentA>CurrentShreholdA(2) &&CurrentA_1min < 1)
            ModeA = 4;  %大于上限
        elseif (CurrentA <CurrentShreholdA(1) && CurrentA_1min >-1)
            if(CurrentA < 115 && ZZ203A < 0.3)
                ModeA = NaN;    %停机、待机单独考虑
            elseif (BallRatio>0.5)
                ModeA = 2;
            else ModeA = 1;
            end
        else
            ModeA = 3;  %无异常则为稳定模式
        end
        %对B进行判断
        if (CurrentB>CurrentShreholdB(2) &&CurrentB_1min < 1)
            ModeB = 4;  %大于上限
        elseif (CurrentB <CurrentShreholdB(1) && CurrentB_1min >-1)
            if(CurrentB < 115 && ZZ203B < 0.3)
                ModeB = NaN;    %停机、待机单独考虑
            elseif (BallRatio>0.5)
                ModeB = 2;
            else ModeB = 1;
            end
        else
            ModeB = 3;  %无异常则为稳定模式
        end
    case 2
        load("CurrentMachineLearning.mat");
        if(CurrentA < 115 && ZZ203A < 0.3)
            ModeA = NaN;    %停机、待机单独考虑
        else
            X_test_A = [CurrentA CurrentA_1min CurrentA_2min CurrentA_3min SpeedSetA CurrentA_ExtraElement];
            Y_predict_A = predict(BestModel_A,X_test_A);
            if(Y_predict_A ==1)
                ModeA = 4;
            elseif (Y_predict_A == 0)
                ModeA = 3;
            elseif (BallRatio>0.5)
                ModeA = 2;
            else ModeA = 1;
            end
        end

        if(CurrentB < 115 && ZZ203B < 0.3)
            ModeB = NaN;    %停机、待机单独考虑
        else
            X_test_B = [CurrentB CurrentB_1min CurrentB_2min CurrentB_3min SpeedSetB CurrentB_ExtraElement];
            Y_predict_B = predict(BestModel_B,X_test_B);
            if(Y_predict_B ==1)
                ModeB = 4;
            elseif (Y_predict_B == 0)
                ModeB = 3;
            elseif (BallRatio>0.5)
                ModeB = 2;
            else ModeB = 1;
            end
        end
    case 3  %工人判断
            %对A进行判断
            if (CurrentA>CurrentA_high &&CurrentA_1min < 1)
                ModeA = 4;  %大于上限
            elseif (CurrentA <CurrentA_low && CurrentA_1min >-1)
                if(CurrentA < 115 && ZZ203A < 0.3)
                    ModeA = NaN;    %停机、待机单独考虑
                elseif (BallRatio>0.5)
                    ModeA = 2;
                else ModeA = 1;
                end
            else
                ModeA = 3;  %无异常则为稳定模式
            end
            %对B进行判断
            if (CurrentB>CurrentB_high&&CurrentB_1min < 1)
                ModeB = 4;  %大于上限
            elseif (CurrentB <CurrentB_low && CurrentB_1min >-1)
                if(CurrentB < 115 && ZZ203B < 0.3)
                    ModeB = NaN;    %停机、待机单独考虑
                elseif (BallRatio>0.5)
                    ModeB = 2;
                else ModeB = 1;
                end
            else
                ModeB = 3;  %无异常则为稳定模式
            end
end

%空转的情况，考虑另一台压球机
if (isnan(ModeA))
    if(isnan(ModeB))
        WaterChange = NaN;
        Status = 6;
        SubStatus = 1;
        return;
    else
        Mode = ModeB;
        flag = 1;
    end
elseif(isnan(ModeB))
    Mode = ModeA;
    flag = 1;
else
    %两台都正常运行的情况
    if(ModeA==ModeB)
        Mode = ModeA;
        flag = 2;
    elseif(ModeA==3&&ModeB~=3)  %一台稳定一台异常
        Mode = ModeB;
        flag = 1;
    elseif(ModeB==3&&ModeA~=3)  %一台稳定一台异常
        Mode = ModeA;
        flag = 1;
    else
        WaterChange = NaN;
        Status = 6;
        SubStatus = 2;
        return;
    end
end
%得到压球机的状态Mode，压球机是否相同的标志位flag

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



switch Mode
    case 1
        if(ReturnBin_10min > 0.2)
            WaterChange = 0.2;
            Status = Mode;
            SubStatus = 1;
            return;
        else
            if(ZZ208 < 0.3)
                WaterChange = 0;
                Status = Mode;
                SubStatus = 2;
                return;
            else
                WaterChange = NaN;
                Status = Mode;
                SubStatus = 3;
                return;
            end
        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    case 2
        score = 0;
        if (BallRatio > 0.65)
            score = score+2;
        end

        if (ZZ208./TotalWeight > 0.55)
            score = score+1;
        end

        if(ReturnBin_10min < -0.5)
            score = score+2;
        end

        score = score*flag;

        if score <2
            WaterChange = 0;
            Status = 2;
            SubStatus = 2;
            return;
        else
            WaterChange = -0.05*score;
            Status = 2;
            SubStatus = 1;
            return;
        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    case 3
        if(ReturnBin > 37 ||ReturnBin <2 ||ReturnBin_10min > 0.4)
            WaterChange = NaN;
            Status = Mode;
            SubStatus = 1;
            return;
        else
            if(ZZ208 ==0)
                WaterChange = NaN;
                Status = Mode;
                SubStatus = 2;
                return;
            else
                WaterChange = 0;
                Status = Mode;
                SubStatus = 3;
                return;
            end
        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    case 4
        score = 0;
        if (BallRatio < 0.56)
            score = score+2;
        end

        if (ZZ208./TotalWeight < 0.56)
            score = score + 1;
        end

        if(ReturnBin_10min > 0.05)
            score = score +2;
        end

        score = score*flag;

        if(score <2)
            WaterChange = 0;
            Status = 4;
            SubStatus = 2;
            return;
        else
            WaterChange = 0.05*score;
            Status = 4;
            SubStatus = 1;
            return;
        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

end